// pages/cate2/cate2.js
let that = null;
Page({
  data:{
    cate:[],
    list:[],

    listId:0,
    listPage:1,

    loadShow:false

  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数    
    that = this;  
    wx.showNavigationBarLoading();
    wx.request({
          url: 'http://shop.heimicms.com/app/cms/cate_list_two',
          data: {
            pid:options.id
          },
          method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
          header: { 'content-type': 'application/json'}, // 设置请求的 header
          success: function(res){
            that.setData({
              cate:res.data.data,
              listId:res.data.data[0].id,
              listPage:1              
            });
            that.refreshList();
          },
          fail: function(res) {
            // fail
          },
          complete: function(res) {            
            wx.hideNavigationBarLoading();
            wx.setNavigationBarTitle({
              title: options.cata_name
            })
          }
     });    
  },
  onReachBottom:function(){
      that.setData({
        loadShow:true,
        listPage:that.data.listPage + 1
      })
      wx.request({
      url: 'http://shop.heimicms.com/app/cms/article_list',
      data: {
        cid:that.data.listId,
        p:that.data.listPage 
      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {'content-type': 'application/json'}, // 设置请求的 header
      success: function(res){ 
        let tempList = res.data.data.list;
        if(tempList.length > 0){
          that.setData({
            list:that.data.list.concat(tempList)
          });
        }else{
          wx.showToast({
            title: '没有更多数据',
            icon: 'success',
            duration: 1000
          });
          that.setData({
            listPage:that.data.listPage - 1
          })
        }  
      },
      complete: function(res) {
        console.log('complete');
        that.setData({
          loadShow:false
        })
      }
    })
  },
  clickCate:function(event){    
    this.setData({
      listId:event.target.id,
      listPage:1
    });
    this.refreshList();
  },
  refreshList:function(){

    wx.showLoading({
      title:'加载中'
    });    
    wx.request({
      url: 'http://shop.heimicms.com/app/cms/article_list',
      data: {
        cid:that.data.listId,
        p:that.data.listPage
      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {'content-type': 'application/json'}, // 设置请求的 header
      success: function(res){        
        that.setData({
          list:res.data.data.list
        })
      },
      fail: function(res) {
        // fail
      },
      complete: function(res) {
        wx.hideLoading();
      }
    })
  }

})